package models

import (
	orm "go-papers/database"
	"go-papers/tools"
	_ "time"
)

type OrderShareLevel struct {
	Id             int    `json:"id" gorm:"type:int(11) unsigned;primary_key"` //
	OrderId        int    `json:"orderId" gorm:"type:int(11);"`                // 订单id
	MomUserId      int    `json:"momUserId" gorm:"type:int(11);"`              // 社区达人
	MomLevelRate   string `json:"momLevelRate" gorm:"type:decimal(10,2);"`     // 社区达人每个等级比例
	MomLevel       int    `json:"momLevel" gorm:"type:int(11);"`               // 当前分佣金等级
	MomLevelPrice  string `json:"momLevelPrice" gorm:"type:decimal(10,2);"`    // 等级可分钱
	MomShareCreate string `json:"momShareCreate" gorm:"type:varchar(255);"`    // 创建时间
	LevelPrice     string `json:"levelPrice" gorm:"type:decimal(10,2);"`       // 等级总可分金额
	LevelStatus    string `json:"levelStatus" gorm:"type:varchar(2);"`         // 状态  2 无效 1有效
	DataScope      string `json:"dataScope" gorm:"-"`
	Params         string `json:"params"  gorm:"-"`
	BaseModel
}

func (OrderShareLevel) TableName() string {
	return "order_share_level"
}

// 创建OrderShareLevel
func (e *OrderShareLevel) Create() (OrderShareLevel, error) {
	var doc OrderShareLevel
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取OrderShareLevel
func (e *OrderShareLevel) Get() (OrderShareLevel, error) {
	var doc OrderShareLevel
	table := orm.Eloquent.Table(e.TableName())
	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.MomUserId != 0 {
		table = table.Where("mom_user_id = ?", e.MomUserId)
	}
	if e.MomLevelRate != "" {
		table = table.Where("mom_level_rate = ?", e.MomLevelRate)
	}
	if e.MomLevel != 0 {
		table = table.Where("mom_level = ?", e.MomLevel)
	}
	if e.MomLevelPrice != "" {
		table = table.Where("mom_level_price = ?", e.MomLevelPrice)
	}
	if e.MomShareCreate != "" {
		table = table.Where("mom_share_create = ?", e.MomShareCreate)
	}
	if e.LevelPrice != "" {
		table = table.Where("level_price = ?", e.LevelPrice)
	}
	if e.LevelStatus != "" {
		table = table.Where("level_status = ?", e.LevelStatus)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrderShareLevel带分页
func (e *OrderShareLevel) GetPage(pageSize int, pageIndex int) ([]OrderShareLevel, int, error) {
	var doc []OrderShareLevel

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.MomUserId != 0 {
		table = table.Where("mom_user_id = ?", e.MomUserId)
	}
	if e.MomLevelRate != "" {
		table = table.Where("mom_level_rate = ?", e.MomLevelRate)
	}
	if e.MomLevel != 0 {
		table = table.Where("mom_level = ?", e.MomLevel)
	}
	if e.MomLevelPrice != "" {
		table = table.Where("mom_level_price = ?", e.MomLevelPrice)
	}
	if e.MomShareCreate != "" {
		table = table.Where("mom_share_create = ?", e.MomShareCreate)
	}
	if e.LevelPrice != "" {
		table = table.Where("level_price = ?", e.LevelPrice)
	}
	if e.LevelStatus != "" {
		table = table.Where("level_status = ?", e.LevelStatus)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrderShareLevel
func (e *OrderShareLevel) Update(id int) (update OrderShareLevel, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrderShareLevel
func (e *OrderShareLevel) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&OrderShareLevel{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *OrderShareLevel) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&OrderShareLevel{}).Error; err != nil {
		return
	}
	Result = true
	return
}
